/*
 * @lc app=leetcode.cn id=414 lang=cpp
 *
 * [414] 第三大的数
 */

#include <limits.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

// @lc code=start
class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long first = INT64_MIN;
        long second = INT64_MIN;
        long third = INT64_MIN;
        
        // 遍历该数字，更新前三大的数字
        for (int i = 0; i < nums.size(); i++) {
            // 如果该数字是之前已经重复过的最大数字，直接跳过
            if (nums[i] == first || nums[i] == second || nums[i] == third) {
                continue;
            }

            // 如果该数字不是最大的前三个数字，直接跳过
            if (nums[i] < third) {
                continue;
            }
            
            if (nums[i] > first) {
                third = second;
                second = first;
                first = nums[i];
            } else if (nums[i] < first && nums[i] > second) {
                third = second;
                second = nums[i];
            } else if (nums[i] < second && nums[i] > third) {
                third = nums[i];
            }
        }

        // 如果第三大的数字不存在，返回最大的数字
        if (third == INT64_MIN) {
            return first;
        }
        
        return third;
    }
};
// @lc code=end

